﻿using System;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using Notepad_X;
using Notepad_X.Classes;

namespace TranslationEditorPlugin
{
    public partial class Editor : Form
    {
        string file = null;
        Main main;
        public Editor()
        {
            main = Notepad_X.Static.MainInstance;
            InitializeComponent();
        }
        private void NewTranslation(bool values, System.Collections.Hashtable language)
        {
            List.Items.Clear();
            file = null;
            Info_Author.Text = null;
            Info_Version.Text = null;
            Info_Language.Text = null;

            foreach (System.Collections.DictionaryEntry entry in language)
            {
                if (entry.Key.ToString() != "none")
                {
                    ListViewItem ListItem = new ListViewItem();
                    ListItem.Name = entry.Key.ToString();
                    ListItem.Text = entry.Key.ToString();
                    if (values == true)
                    {
                        if (entry.Value != null)
                        {
                            ListItem.SubItems.Add(entry.Value.ToString());
                        }
                        else
                        {
                            ListItem.SubItems.Add("");
                        }
                    }
                    else
                    {
                        ListItem.SubItems.Add("");
                    }
                    List.Items.Add(ListItem);
                }
            }
            UpdateMenu();
        }


        private void Editor_Load(object sender, EventArgs e)
        {
            if (Main.app_settings.language.ToLower() == "default")
            {
                NewTranslation(false, Main.app_language.strings);

            }
            else
            {
                NewTranslation(true, Main.app_language.strings);

                Info_Author.Text = Main.app_language.Author;
                Info_Version.Text = Main.app_language.Version;
                Info_Language.Text = Main.app_language.language;

                file = Main.System_ROOT + "\\Translations\\" + Main.app_settings.language;
            }

            if (main.IconsCollection.Images.ContainsKey("new"))
            {
                _new.Image = main.IconsCollection.Images["new"];
            }
            if (main.IconsCollection.Images.ContainsKey("open"))
            {
                _open.Image = main.IconsCollection.Images["open"];
            }
            if (main.IconsCollection.Images.ContainsKey("save"))
            {
                _save.Image = main.IconsCollection.Images["save"];
            }
            if (main.IconsCollection.Images.ContainsKey("saveas"))
            {
                _saveas.Image = main.IconsCollection.Images["saveas"];
            }

        }
        private void List_SelectedIndexChanged(object sender, EventArgs e)
        {
            Editor_OK.Enabled = false;
            if (List.SelectedItems.Count != 0)
            {
                ListViewItem ListItem;
                ListItem = List.SelectedItems[0];
                Editor_String.Text = ListItem.Text;
                Editor_Translated.Text = ListItem.SubItems[1].Text;
                Editor_Translated.SelectAll();
                Editor_Translated.Focus();
                Editor_OK.Enabled = true;
            }

        }

        private void save_file(string path)
        {
            file = path;
            XmlTextWriter xmlFile = new XmlTextWriter(path, System.Text.Encoding.UTF8);
            xmlFile.Formatting = Formatting.Indented;
            xmlFile.WriteStartElement("NotepadX");

            xmlFile.WriteStartElement("Information");

            xmlFile.WriteStartElement("Author");
            xmlFile.WriteString(Info_Author.Text);
            xmlFile.WriteEndElement();

            xmlFile.WriteStartElement("Version");
            xmlFile.WriteString(Info_Version.Text);
            xmlFile.WriteEndElement();

            xmlFile.WriteStartElement("Language");
            xmlFile.WriteString(Info_Language.Text);
            xmlFile.WriteEndElement();

            xmlFile.WriteEndElement();

            foreach (ListViewItem item in List.Items)
            {
                xmlFile.WriteStartElement("String");
                xmlFile.WriteAttributeString("id", item.Text);
                xmlFile.WriteString(item.SubItems[1].Text);
                xmlFile.WriteEndElement();
            }

            xmlFile.WriteEndElement();
            xmlFile.Flush();
            xmlFile.Close();

        }

        private void saveas()
        {
            SaveFileDialog Dialog = new SaveFileDialog();
            Dialog.Filter = "XML Files (*.xml)|*.xml";
            Dialog.Title = "Save translation file";
            Dialog.FileName = Info_Language.Text;
            if (Dialog.ShowDialog() == DialogResult.OK)
            {
                save_file(Dialog.FileName);
            }

        }
        private void UpdateMenu()
        {
            if (!String.IsNullOrEmpty(List.Items["New"].SubItems[1].Text))
            {
                _new.Text = List.Items["New"].SubItems[1].Text;
            }
            else
            {
                _new.Text = "New";
            }
            if (!String.IsNullOrEmpty(List.Items["Open"].SubItems[1].Text))
            {
                _open.Text = List.Items["Open"].SubItems[1].Text;
            }
            else
            {
                _open.Text = "Open";
            }
            if (!String.IsNullOrEmpty(List.Items["Save"].SubItems[1].Text))
            {
                _save.Text = List.Items["Save"].SubItems[1].Text;
            }
            else
            {
                _save.Text = "Save";
            }
            if (!String.IsNullOrEmpty(List.Items["SaveAs"].SubItems[1].Text))
            {
                _saveas.Text = List.Items["SaveAS"].SubItems[1].Text;
            }
            else
            {
                _saveas.Text = "Save As";
            }
        }
        private void change()
        {
            if (List.SelectedItems.Count > 0)
            {
                List.SelectedItems[0].SubItems[1].Text = Editor_Translated.Text;
                List.Focus();
            }
            UpdateMenu();
        }
        private void Editor_OK_Click(object sender, EventArgs e)
        {
            change();
        }
        private void Editor_Translated_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (e.KeyChar == (char)13)
            {
                change();
            }
        }

        private void _new_Click(object sender, EventArgs e)
        {
            NewTranslation(false, Main.app_language.strings);
        }

        private void _import_Click(object sender, EventArgs e)
        {
            OpenFileDialog Dialog = new OpenFileDialog();
            Dialog.Filter = "XML Files (*.xml)|*.xml";
            Dialog.Title = "Open a translation .xml file";
            Dialog.InitialDirectory = Main.System_ROOT + " \\Translations";
            Dialog.Multiselect = false;
            if (Dialog.ShowDialog() == DialogResult.OK)
            {
                if (Language.hashTranslation(Dialog.FileName) != null)
                {
                    file = Dialog.FileName;
                    Language newLanguage = new Language();
                    newLanguage.set_default();
                    newLanguage.ReadLanguageFile(Dialog.FileName);
                    NewTranslation(true, newLanguage.strings);
                    Info_Author.Text = newLanguage.Author;
                    Info_Language.Text = newLanguage.language;
                    Info_Version.Text = newLanguage.Version;
                    file = Dialog.FileName;
                }
            }
        }

        private void _save_Click(object sender, EventArgs e)
        {
            if (file == null)
            {
                saveas();
            }
            else
            {
                save_file(file);
            }
        }

        private void _saveas_Click(object sender, EventArgs e)
        {
            saveas();
        }

        private void _import_Click_1(object sender, EventArgs e)
        {
            OpenFileDialog Dialog = new OpenFileDialog();
            Dialog.Filter = "XML Files (*.xml)|*.xml";
            Dialog.Title = "Open a translation .xml file";
            Dialog.Multiselect = false;
            if (Dialog.ShowDialog() == DialogResult.OK)
            {

                XmlDocument XMLDoc = new XmlDocument();
                XMLDoc.Load(Dialog.FileName);

                Info_Author.Text = XMLDoc.SelectSingleNode("Lanquage/General/Author").InnerText;
                Info_Version.Text = XMLDoc.SelectSingleNode("Lanquage/General/Version").InnerText;
                Info_Language.Text = XMLDoc.SelectSingleNode("Lanquage/General/LanguageName").InnerText;

                System.Collections.Hashtable Language = new System.Collections.Hashtable();

                foreach (String key in Enum.GetNames(typeof(Strings)))
                {
                    Language.Add(key, null);
                }

                foreach (XmlNode node in XMLDoc.SelectSingleNode("Lanquage/Menu").ChildNodes)
                {
                    string id = node.Name;
                    if (Language.ContainsKey(id))
                    {
                        Language[id] = node.InnerText;
                    }
                }

                NewTranslation(true, Language);
                file = null;
            }
        }



    }

    }

